<template>
  <div id="app">
    <!-- 头部 -->
    <mt-header fixed title="黑马程序员·Vue项目">
      <span slot="left" v-show="flag">
        <mt-button icon="back" @click="back()">返回</mt-button>
      </span>
    </mt-header>

    <!-- 中间 -->
    <transition mode="out-in">
      <router-view/>
    </transition>

    <!-- 底部 -->
    <nav class="mui-bar mui-bar-tab">
      <router-link class="mui-tab-item" to="/home">
        <span class="mui-icon mui-icon-home"></span>
        <span class="mui-tab-label">首页</span>
      </router-link>
      <router-link class="mui-tab-item" to="/member">
        <span class="mui-icon mui-icon-contact"></span>
        <span class="mui-tab-label">会员</span>
      </router-link>
      <router-link class="mui-tab-item" to="/shopcar">
        <span class="mui-icon mui-icon-extra mui-icon-extra-cart">
          <span class="mui-badge">9</span>
        </span>
        <span class="mui-tab-label">购物车</span>
      </router-link>
      <router-link class="mui-tab-item" to="/search">
        <span class="mui-icon mui-icon-search"></span>
        <span class="mui-tab-label">搜索</span>
      </router-link>
    </nav>
  </div>
</template>

<script>
export default {
  created() {
    // 页面刷新的时候，需要重新判断路径
    this.nowUrl === '/home' ? (this.flag = false) : (this.flag = true)
  },
  data() {
    return {
      flag: false,
      nowUrl: this.$route.path
    }
  },
  methods: {
    back() {
      // console.log(this.$route)
      console.log(this.nowUrl)
      this.$router.go(-1)
    }
  },
  // 通过watch方法，监听某一数据变化，并做响应
  watch: {
    '$route.path': function(nowUrl) {
      // if (nowUrl === '/home') {
      //   this.flag = false
      // } else {
      //   this.flag = true
      // }
      nowUrl === '/home' ? (this.flag = false) : (this.flag = true)
    }
  }
}
</script>

<style>
#app {
  padding-top: 40px;
  /* 消除动画过渡是产生的滚动条 */
  overflow: hidden;
}
/* 配置router-view的过渡动画 */
.v-enter-active, .v-leave-active {
  transition: all .5s ease;
}
.v-enter {
  transform: translateX(100%);
  opacity: 0;
}
.v-leave-to {
  transform: translateX(-100%);
  opacity: 0;
  padding-bottom: 50px;
}
</style>
